package cn.edu.scnu.controller;

import cn.edu.scnu.entity.Movie;
import cn.edu.scnu.result.Result;
import cn.edu.scnu.result.ResultUtil;
import cn.edu.scnu.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EChartsController {
    @Autowired
    private MovieService movieService;

    // 生成电影地区饼状图
    @RequestMapping("showAreaChart")
    @ResponseBody
    public Result showAreaChart() {
        // 查询所有电影
        List<Movie> movieList = movieService.showAllMovie();
        // 所有电影地区
        List<String> areaList = List.of("内地", "港澳台", "韩国", "日本", "欧美", "其他");

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (String movieArea : areaList) {
            Map<String, Object> map = new HashMap<>();
            int areaCount = 0;
            // 获取电影的地区
            for (Movie movie: movieList) {
                // 如果电影属于该地区
                if (movieArea.equals(movie.getMovieArea())) {
                    areaCount++;
                }
            }
            // 前端所需数据
            map.put("movieArea", movieArea);
            map.put("areaCount", areaCount);
            mapList.add(map);
        }
        return ResultUtil.success(mapList);
    }

    // 生成电影类型柱状图
    @RequestMapping("showTypeChart")
    @ResponseBody
    public Result showTypeChart() {
        // 查询所有电影
        List<Movie> movieList = movieService.showAllMovie();
        // 所有电影类型
        List<String> typeList = List.of("爱情", "动画", "悬疑", "喜剧", "动作", "其他");

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (String movieType : typeList) {
            Map<String, Object> map = new HashMap<>();
            int typeCount = 0;
            // 获取电影的类型
            for (Movie movie: movieList) {
                // 如果电影属于该类型
                if (movieType.equals(movie.getMovieType())) {
                    typeCount++;
                }
            }
            // 前端所需数据
            map.put("movieType", movieType);
            map.put("typeCount", typeCount);
            mapList.add(map);
        }
        return ResultUtil.success(mapList);
    }
}
